
//
//  UManager.m
//  IMChat
//
//  Created by Admin on 2021/6/30.
//  Copyright © 2021年 TeamOne. All rights reserved.
//

#import "UManager.h"

@implementation UManager

//增加User的信息
- (void)insert
{
    
    //1.取出NSManagedObjectContext.
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    //2.通过上下文,新建一个CoreData Entity模型.  ///此代码等价于 ==  类的 alloc init
    UserEntity *user = [NSEntityDescription insertNewObjectForEntityForName:@"UserEntity"
                                                     inManagedObjectContext:delegate.persistentContainer.viewContext];
    //3.进行属性设置
    user.token = token;
    user.user_name=user_name;
    user.user_password=user_password;
    user.user_portraitUri=user_portraitUri;
    user.userId=userId;
    
    ///打印
    NSLog(@"用户信息为 token是:%@ 名字是:%@ 密码是:%@ portraitUri是:%@  Id是:%@ ,增加成功",user.token, user.user_name,user.user_password,user.user_portraitUri,user.userId);
    
    //4.保存上下文，保存到数据库
    [delegate saveContext];
    
    
}

// 2.查找某一个User的信息(UserId)
-(void)query
{
    //1.取出NSManagedObjectContext.
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    //2.通过上下文,新建一个CoreData Entity模型.数据库读取这个表
    NSEntityDescription *user = [NSEntityDescription entityForName:@"UserEntity"
                                            inManagedObjectContext:delegate.persistentContainer.viewContext];
    
    ///创建查询请求
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    ///设置查询请求的 实体
    [request setEntity:user];
    ///这是查询条件:UserID
    //        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userId=%@",userId];
    //        [request setPredicate:predicate];
    
    ///获取查询的结果
    NSArray *resultArray = [delegate.persistentContainer.viewContext executeFetchRequest:request
                                                                                   error:nil];
    ///打印查询结果
    for (UserEntity *user in resultArray) {
        NSLog(@"用户对信息为 token是:%@ 名字是:%@ 密码是:%@ portraitUri是:%@  Id是:%@",user.token, user.user_name,user.user_password,user.user_portraitUri,user.userId);
    }
    
    NSLog(@"查询用户信息完成");
}


//修改：直接修改模型对象后通过context保存即可
-(void)update
{
    //1.取出NSManagedObjectContext.
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    //2.通过上下文,新建一个CoreData Entity模型.
    ///读取所有学生的实体
    NSEntityDescription *user = [NSEntityDescription entityForName:@"UserEntity"
                                            inManagedObjectContext:delegate.persistentContainer.viewContext];
    //    //创建请求（同查，查出来才能删）
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request setEntity:user];
    
    ///这是查询条件:UserID
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userId=%@",userId];
    [request setPredicate:predicate];
    
    ///获取符合条件的结果
    NSArray *resultArray = [delegate.persistentContainer.viewContext executeFetchRequest:request
                                                                                   error:nil];
    if (resultArray.count>0) {
        for (UserEntity *user in resultArray) {
            ///把名字改为李四
            user.user_name= @"李四";
            ///并且把名字添加一个修
            user.user_name = [NSString stringWithFormat:@"%@修",user.user_name];
        }
        ///保存结果并且打印
        [delegate saveContext];
        NSLog(@"修改用户信息完成");
    }else{
        NSLog(@"没有符合条件的结果");
    }
    
    
    
}


//删除一个实体对象
-(void)datadelete
{
    //1.取出NSManagedObjectContext.
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    //2.通过上下文,新建一个CoreData Entity模型.
    ///读取所有学生的实体
    NSEntityDescription *user = [NSEntityDescription entityForName:@"UserEntity"
                                            inManagedObjectContext:delegate.persistentContainer.viewContext];
    //创建请求（同查，查出来才能删）
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request setEntity:user];
    
    //这是查询条件:UserID
    //    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userId=%@",userId];
    //    [request setPredicate:predicate];
    ///获取符合条件的结果
    NSArray *resultArray = [delegate.persistentContainer.viewContext executeFetchRequest:request
                                                                                   error:nil];
    if (resultArray.count>0) {
        for (UserEntity *user in resultArray) {
            ///删除实体
            [delegate.persistentContainer.viewContext deleteObject:user];
        }
        ///保存结果并且打印
        [delegate saveContext];
        NSLog(@"删除用户完成");
    }else{
        NSLog(@"没有符合条件的结果");
    }
    
    
    
    
}
@end
